package com.example.droideep.fragment;


import android.os.Bundle;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow.OnDismissListener;

import com.example.droideep.R;
import com.example.droideep.common.activities.SimpleFragmentBase;
import com.example.droideep.common.view.ArrowTagStrip;
import com.example.droideep.common.view.CalendarDropDownPopup;
import com.example.droideep.common.view.DropDownPopup;
import com.example.droideep.common.view.ListDropDownPopup;

import droideep.library.utils.DLog;
import droideep.library.widget.TagStrip;

/**
 * A simple {@link Fragment} subclass.
 */
public class PopupWindowFragment extends SimpleFragmentBase implements OnDismissListener {

    private DropDownPopup popupWindow0;
    private DropDownPopup popupWindow1;

    private ArrowTagStrip tagStrip;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    protected int getContentResId() {
        return R.layout.fragment_popup_window;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        tagStrip = (ArrowTagStrip) view.findViewById(R.id.tagStrip);
        tagStrip.setTags(getResources().getStringArray(R.array.connections));
        tagStrip.setPositionListener(new TagStrip.TabPositionListener() {
            @Override
            public void onTab(int position) {
                if (!tagStrip.isItemChecked(position)) {
                    showPopup(position);
                }
            }
        });
        popupWindow0 = new ListDropDownPopup(getActivity());
        popupWindow1 = new CalendarDropDownPopup(getActivity());

        popupWindow0.setOnDismissListener(this);
        popupWindow1.setOnDismissListener(this);
    }

    private void showPopup(int position) {
        DLog.e(TAG, "打开-->" + position);
        switch (position) {
            case 0:
                popupWindow0.showAsDropDown(tagStrip);
                break;
            case 1:
                popupWindow1.showAsDropDown(tagStrip);
                break;
        }
    }

    public void dismissPopup(int position) {
        DLog.e(TAG, "关闭-->" + position);
        switch (position) {
            case 0:
                popupWindow0.dismiss();
                break;
            case 1:
                popupWindow1.dismiss();
                break;
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (popupWindow0.isShowing()) {
            popupWindow0.dismiss();
        }

        if (popupWindow1.isShowing()) {
            popupWindow1.dismiss();
        }

    }

    @Override
    public void onDismiss() {
        tagStrip.setAllItemNoChecked();
    }
}
